//
//  HELCourseHeader.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/16.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCourseHeader.h"
#import "UIImageView+BTimageView.h"
@implementation HELCourseHeader



+ (instancetype)sharedHeader{
    return [[NSBundle mainBundle] loadNibNamed:@"HELCourseHeader" owner:nil options:0].lastObject;
}

- (IBAction)priseAction:(UIButton *)sender {
    
  
    [self animationWith:sender.imageView.layer];
  
    [self priseCourse:sender.selected];
    
}

- (void)resetBtn:(UIButton *)sender{
  
        sender.selected = !sender.selected;
        if (sender.tag ==300) {
            if (self.priseBlock) {
                self.priseBlock(sender.selected);
            }
        }else{
            if (self.collectBlock) {
                self.collectBlock(sender.selected);
            }
        }
       
  


}

- (IBAction)collectAction:(UIButton *)sender {
    
    [self animationWith:sender.imageView.layer];
    [self collectCourse:sender.selected];
  
}


- (void)animationWith:(CALayer *)layer{
    [UIView animateWithDuration:.8
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         //你想绕哪个轴哪个轴就为 1，其中的参数（角度， x, y, z）
                        
                         layer.affineTransform = CGAffineTransformScale(layer.affineTransform, 6, 6);
                         
                         
                     }
                     completion:^(BOOL finished) {
                          layer.affineTransform = CGAffineTransformIdentity;
                         
                     }];
}


- (void)setCourseDetail:(HELCourseDetailModel *)courseDetail{
  
    _courseDetail = courseDetail;
    
    CGFloat f = kScreenFactor;
    self.picHight.constant = f * 168;
    
    self.titleL.text = courseDetail.courseName;
    self.commentL.text = [NSString stringWithFormat:@"%@人评价",courseDetail.commentNumber];
    [self.mainPic nc_setImageWithUrl:courseDetail.courseImage];
    self.priseB.selected =  [courseDetail.isLike integerValue]?YES:NO;
    self.collectBtn.selected =[courseDetail.isCollect integerValue]?YES:NO;
    [self.priseB setTitle:[NSString stringWithFormat:@"%@",courseDetail.likeNumber] forState:UIControlStateNormal ];
    self.statuL.text =courseDetail.status;
    [self.priseB setTitle:[NSString stringWithFormat:@"%@",courseDetail.likeNumber] forState:UIControlStateSelected];
    [self.collectBtn setTitle:[NSString stringWithFormat:@"%@",courseDetail.collectNumber] forState:UIControlStateNormal];
       [self.collectBtn setTitle:[NSString stringWithFormat:@"%@",courseDetail.collectNumber] forState:UIControlStateSelected];
    self.lerningCount.text = [NSString stringWithFormat:@"%@人在线学习",courseDetail.studyNumber];
    self.ratingView.value = [courseDetail.courseScoreAvg floatValue]/2;
    
    self.priceL.text =[NSString stringWithFormat:@"¥%.2f",[courseDetail.coursePrice floatValue]];
    
    if ([courseDetail.coursePrice floatValue] <=0 ) {
        self.priceL.text = @"";
    }else{
        self.priceL.text = [NSString stringWithFormat:@"¥%.2f",[courseDetail.coursePrice floatValue] ];
    }
    self.scoreTypeL.text = [NSString stringWithFormat:@"%@",courseDetail.creditName];
    self.scoreNumL.text =[NSString stringWithFormat:@"%@分",courseDetail.credit];
    
    [self setNeedsLayout];
    
    
 
}

- (void)priseCourse:(BOOL)isPrise{
 
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"courseId"] = self.courseDetail.id;
    params[@"isLike"] = @(isPrise);
    weakSelf_define;
    [[HELNetworkManager sharedMg] PUT:kPriseCourse parameters:params progress:nil success:^(id response) {
        if ([response[@"data"] isKindOfClass:[NSString class]]) {
           
             [weakSelf resetBtn:weakSelf.priseB];
        }
       
    } failure:^(NSString *err) {
        
    }];

}

- (void)collectCourse:(BOOL)isCollect{
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"courseId"] = self.courseDetail.id;
    params[@"isCollect"] = @(isCollect);
    weakSelf_define;
    [[HELNetworkManager sharedMg] PUT:kCourseCollect_url parameters:params progress:nil success:^(id response) {
        if ([response[@"data"] isKindOfClass:[NSString class]]) {
            
            [weakSelf resetBtn:weakSelf.collectBtn];
        }
        
    } failure:^(NSString *err) {
        
    }];
    
}






@end
